import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _uniq from "lodash/uniq";
import _sortBy from "lodash/sortBy";
import _pull from "lodash/pull";
import _isUndefined from "lodash/isUndefined";
import _isNumber from "lodash/isNumber";
import _isEmpty from "lodash/isEmpty";
import _isArray from "lodash/isArray";
import { PAGE_SIZE_OPTIONS } from "../constants";
import { totalRender } from "../renderer";
/**
 * 根据传入的数据列表对象，生成页面分页参数对象
 * @param {object} data - 数据列表对象
 * @returns {object} pagination- 分页对象
 */

export function createPagination(data, list) {
  if (data) {
    return {
      showSizeChanger: true,
      pageSizeOptions: _isArray(list) && !_isEmpty(list) ? list : PAGE_SIZE_OPTIONS,
      // showQuickJumper: true,
      current: (_isNumber(data.number) ? data.number : data.start) + 1,
      pageSize: data.size,
      // 每页大小
      total: _isNumber(data.totalElements) ? data.totalElements : data.total,
      showTotal: totalRender
    };
  }
}
/**
 * 表格批量新增操作, 添加数据行，更新分页信息
 * @param {number} length - 数据列表长度
 * @param {object} pagination - 原始分页对象
 * @returns {object} pagination - 分页对象
 */

export function addItemToPagination(length) {
  var pagination = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  var _pagination$total = pagination.total,
      total = _pagination$total === void 0 ? 0 : _pagination$total,
      _pagination$pageSize = pagination.pageSize,
      pageSize = _pagination$pageSize === void 0 ? 10 : _pagination$pageSize,
      _pagination$current = pagination.current,
      current = _pagination$current === void 0 ? 1 : _pagination$current,
      sourceSize = pagination.sourceSize,
      _pagination$pageSizeO = pagination.pageSizeOptions,
      pageSizeOptions = _pagination$pageSizeO === void 0 ? PAGE_SIZE_OPTIONS : _pagination$pageSizeO;
  var size = length === pageSize ? pageSize + 1 : pageSize;
  var source = _isUndefined(sourceSize) ? pageSize : sourceSize;

  var pages = _uniq([].concat(_toConsumableArray(_pull(_toConsumableArray(pageSizeOptions), "".concat(pageSize))), ["".concat(size)]));

  return _objectSpread(_objectSpread({}, pagination), {}, {
    // 变更每页显示条数
    pageSizeOptions: _sortBy(pages, function (item) {
      return +item;
    }),
    // 更新数据总量
    total: total + 1,
    // 记录pageSize的原始值
    sourceSize: source,
    // 根据 数据列表的长度与每页大小的情况，更新分页大小
    pageSize: size,
    // 变更showTotal信息
    showTotal: function showTotal() {
      return totalRender(total + 1, [source * (current - 1) + 1, source * (current - 1) + size < total + 1 ? source * (current - 1) + size : total + 1]);
    }
  });
}
/**
 * 表格批量新增操作, 移除数据行，更新分页信息
 * @param {number} length - 数据列表长度
 * @param {object} pagination - 原始分页对象
 * @returns {object} pagination - 分页对象
 */

export function delItemToPagination(length) {
  var pagination = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  var _pagination$total2 = pagination.total,
      total = _pagination$total2 === void 0 ? 1 : _pagination$total2,
      _pagination$pageSize2 = pagination.pageSize,
      pageSize = _pagination$pageSize2 === void 0 ? 10 : _pagination$pageSize2,
      _pagination$current2 = pagination.current,
      current = _pagination$current2 === void 0 ? 1 : _pagination$current2,
      sourceSize = pagination.sourceSize,
      _pagination$pageSizeO2 = pagination.pageSizeOptions,
      pageSizeOptions = _pagination$pageSizeO2 === void 0 ? PAGE_SIZE_OPTIONS : _pagination$pageSizeO2;
  var size = length === pageSize ? pageSize - 1 : pageSize;

  var pages = _uniq([].concat(_toConsumableArray(_pull(_toConsumableArray(pageSizeOptions), "".concat(pageSize))), ["".concat(size)]));

  return _objectSpread(_objectSpread({}, pagination), {}, {
    // 更新数据总量
    total: total - 1,
    // 变更每页显示条数
    pageSizeOptions: _sortBy(pages, function (item) {
      return +item;
    }),
    // 根据 数据列表的长度与每页大小的情况，更新分页大小
    pageSize: size,
    // 变更showTotal信息
    showTotal: function showTotal() {
      return totalRender(total - 1, [sourceSize * (current - 1) + 1, sourceSize * (current - 1) + size < total - 1 ? sourceSize * (current - 1) + size : total - 1]);
    }
  });
}
/**
 * 表格批量新增操作, 添加数据行，更新分页信息
 * @param {number} addItemsLength - 新增数据长度
 * @param {number} currentLength - 当前数据长度
 * @param {object} pagination - 原始分页对象
 * @returns {object} pagination - 分页对象
 */

export function addItemsToPagination(addItemsLength) {
  var currentLength = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
  var pagination = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  var _pagination$total3 = pagination.total,
      total = _pagination$total3 === void 0 ? 0 : _pagination$total3,
      _pagination$pageSize3 = pagination.pageSize,
      pageSize = _pagination$pageSize3 === void 0 ? 10 : _pagination$pageSize3,
      _pagination$current3 = pagination.current,
      current = _pagination$current3 === void 0 ? 1 : _pagination$current3,
      sourceSize = pagination.sourceSize,
      _pagination$pageSizeO3 = pagination.pageSizeOptions,
      pageSizeOptions = _pagination$pageSizeO3 === void 0 ? PAGE_SIZE_OPTIONS : _pagination$pageSizeO3;
  var nextDataLength = currentLength + addItemsLength;
  var nextTotal = total + addItemsLength;
  var size = nextDataLength >= pageSize ? nextDataLength : pageSize;
  var source = _isUndefined(sourceSize) ? pageSize : sourceSize;

  var pages = _uniq([].concat(_toConsumableArray(_pull(_toConsumableArray(pageSizeOptions), "".concat(pageSize))), ["".concat(size)]));

  return _objectSpread(_objectSpread({}, pagination), {}, {
    // 变更每页显示条数
    pageSizeOptions: _sortBy(pages, function (item) {
      return +item;
    }),
    // 更新数据总量
    total: nextTotal,
    // 记录pageSize的原始值
    sourceSize: source,
    // 根据 数据列表的长度与每页大小的情况，更新分页大小
    pageSize: size,
    // 变更showTotal信息
    showTotal: function showTotal() {
      return totalRender(nextTotal, [source * (current - 1) + 1, source * (current - 1) + size < nextTotal ? source * (current - 1) + size : nextTotal]);
    }
  });
}
/**
 * 表格批量新增操作, 移除数据行，更新分页信息
 * @param {number} delItemsLength - 新增数据长度
 * @param {number} currentLength - 当前数据长度
 * @param {object} pagination - 原始分页对象
 * @returns {object} pagination - 分页对象
 */

export function delItemsToPagination(delItemsLength) {
  var currentLength = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
  var pagination = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  var _pagination$total4 = pagination.total,
      total = _pagination$total4 === void 0 ? 0 : _pagination$total4,
      _pagination$pageSize4 = pagination.pageSize,
      pageSize = _pagination$pageSize4 === void 0 ? 10 : _pagination$pageSize4,
      _pagination$current4 = pagination.current,
      current = _pagination$current4 === void 0 ? 1 : _pagination$current4,
      _pagination$sourceSiz = pagination.sourceSize,
      sourceSize = _pagination$sourceSiz === void 0 ? pageSize : _pagination$sourceSiz,
      _pagination$pageSizeO4 = pagination.pageSizeOptions,
      pageSizeOptions = _pagination$pageSizeO4 === void 0 ? PAGE_SIZE_OPTIONS : _pagination$pageSizeO4;
  var nextDataLength = currentLength - delItemsLength;
  var nextTotal = total - delItemsLength;
  var size = Math.max(nextDataLength <= pageSize ? nextDataLength : pageSize, sourceSize);

  var pages = _uniq([].concat(_toConsumableArray(_pull(_toConsumableArray(pageSizeOptions), "".concat(pageSize))), ["".concat(size)]));

  return _objectSpread(_objectSpread({}, pagination), {}, {
    // 更新数据总量
    total: nextTotal,
    // 变更每页显示条数
    pageSizeOptions: _sortBy(pages, function (item) {
      return +item;
    }),
    // 根据 数据列表的长度与每页大小的情况，更新分页大小
    pageSize: size,
    // 变更showTotal信息
    showTotal: function showTotal() {
      return totalRender(nextTotal, [sourceSize * (current - 1) + 1, sourceSize * (current - 1) + size < nextTotal ? sourceSize * (current - 1) + size : nextTotal]);
    }
  });
}